[Feature] Allow TabsTrigger to render as an anchor via as:#425
Merged
Conversation
Add an `as:` option to `RubyUI::TabsTrigger` (default `:button`) so a trigger can render as `<a href>` for tabs that are navigation links. This avoids nesting a `<button>` inside an `<a>` (invalid HTML and a keyboard/accessibility issue) when tabs are server-rendered links. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
1 issue found across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Tie the `type="button"` attribute to the element actually rendered (`unless @as == :a`) instead of an exact `@as == :button` check. An unexpected/mistyped `as:` value fell through to the `<button>` branch without a type, which defaults to `type="submit"` and could trigger accidental form submission. Issue identified by cubic. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds an
as:option toRubyUI::TabsTrigger(default:button) so a trigger can render as an<a href>instead of a<button>.This is useful when tabs are actually navigation links — e.g. server-rendered tabs driven by a query param (
?tab=...). The common workaround is to wrap aTabsTrigger(<button>) inside aLink(<a>), which produces a<button>nested inside an<a>. That is invalid HTML and a keyboard/accessibility problem (nested interactive elements). Withas: :a, each navigable tab collapses to a single<a>while keeping the trigger's styling anddata-*attributes.Behavior
TabsTriggerstill renders<button type="button">, so existing client-side tabs are unaffected (fully backward compatible).TabsTrigger(value: ..., as: :a, href: ...)renders<a href>with the same classes anddata-*(data-ruby-ui--tabs-target="trigger", theclick->ruby-ui--tabs#showaction, anddata-value).type="button"is only emitted for the:buttonvariant (it's invalid on an anchor).The
ruby-ui--tabs#showStimulus controller does not callpreventDefault, so the anchor navigates normally while the active state stays driven by thedata-*attributes.Usage
Changes
gem/lib/ruby_ui/tabs/tabs_trigger.rb— addas:option and conditional element/typerendering.gem/test/ruby_ui/tabs_trigger_test.rb— new tests: renders as<button>by default, renders as<a href>withas: :a, and preserves triggerdata-*on the anchor.docs/app/views/docs/tabs.rb— add an "As navigation links" example.How to test
🤖 Generated with Claude Code
Summary by cubic
Add
as:toRubyUI::TabsTriggerto render triggers as<a href>for navigation tabs; anchors keep trigger classes anddata-*and navigate normally. Default remains<button type="button">, and we now always emittype="button"for any non-anchor trigger to prevent accidental form submissions (e.g., whenas:is mistyped).Written for commit f753dd8. Summary will update on new commits.